package com.oscar.ehome.view.pageview.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.C;
import com.oscar.ehome.bean.pagebean.HdBean;
import com.oscar.ehome.provider.gildeimage.GildeUtils;
import com.oscar.ehome.utils.util.DateUtil;

import java.util.List;

/**
 * Created by oscar
 * author: MengWanhe
 * date: On 2017/7/7
 * 活动列表
 */

public class HdAdapter extends BaseAdapter {

    private List<HdBean.ListBean> list;
    private LayoutInflater mInflater;
    private Context mContext = null;
    IntentListener intentListener;

    public HdAdapter(Context context, List<HdBean.ListBean> list) {
        this.mContext = context;
        this.list = list;
        mInflater = LayoutInflater.from(context);
    }

    public void setLists(List<HdBean.ListBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public interface IntentListener {
        public void onIntentClick(int postion);
    }

    public void setOnINtentClickListener(IntentListener intentListener) {
        this.intentListener = intentListener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_huodong, null);
            holder = new ViewHolder();
            holder.huodong_title = (TextView) convertView.findViewById(R.id.huodong_title);
            holder.hd_stick = (TextView) convertView.findViewById(R.id.hd_stick);
            holder.singin_buton = (Button) convertView.findViewById(R.id.singin_buton);
            holder.hd_img = (ImageView) convertView.findViewById(R.id.hd_img);
            holder.huodong_time = (TextView) convertView.findViewById(R.id.huodong_time);
            holder.update_time = (TextView) convertView.findViewById(R.id.huodong_show_time);
            holder.huodong_show_times = (TextView) convertView.findViewById(R.id.huodong_show_times);
            convertView.setTag(holder); //
        } else {
            holder = (ViewHolder) convertView.getTag(); //
        }

        if (list.size() > 0) {
            if (list.get(position) != null) {
                holder.huodong_title.setText(list.get(position).getTtitle());
                holder.huodong_time.setText(list.get(position).getStartDate());
//                //修改
                //holder.update_time.setText(DateUtil.timeLogic(list.get(position).getUpdateDate()));
                // GildeUtils.loadUrlImage(C.qiniu + list.get(position).getShowimg(), holder.hd_img, mContext);
                if (list.get(position).getIsSticky().equals("1")) {
                    holder.hd_stick.setVisibility(View.VISIBLE);
                } else {
                    holder.hd_stick.setVisibility(View.GONE);
                }

                if (!("").equals(list.get(position).getEventBigimg()) || !((list.get(position).getEventBigimg()) == null)) {
//                    GildeUtils.imgLoadPic(mContext, C.qiniu + list.get(position).getEventBigimg(), holder.hd_img, "net");
                    GildeUtils.loadUrlImages(C.qiniu + list.get(position).getEventBigimg(), holder.hd_img, mContext);
                }
                if (((list.get(position).getEventBigimg() == null) || ("").equals(list.get(position).getEventBigimg()))) {
//                    GildeUtils.imgLoadPic(mContext, C.qiniu + list.get(position).getEventImg(), holder.hd_img, "net");
                    GildeUtils.loadUrlImages(C.qiniu + list.get(position).getEventImg(), holder.hd_img, mContext);
                }
                if (((("".equals(list.get(position).getEventBigimg()) || (list.get(position).getEventBigimg()) == null)) && (("".equals(list.get(position).getEventImg()) || (list.get(position).getEventImg()) == null)))) {
//                    GildeUtils.imgLoadPic(mContext, C.qiniu + list.get(position).getEventDefaultimg(), holder.hd_img, "net");
                    GildeUtils.loadUrlImages(C.qiniu + list.get(position).getEventDefaultimg(), holder.hd_img, mContext);
                }

                //0否1是
                if ("0".equals(list.get(position).getIsManager())) {
                    holder.update_time.setVisibility(View.GONE);
                    holder.update_time.setText(DateUtil.timeLogic(list.get(position).getUpdateDate()));
                    holder.huodong_show_times.setVisibility(View.VISIBLE);
                    holder.huodong_show_times.setText(DateUtil.timeLogic(list.get(position).getUpdateDate()));
                    holder.singin_buton.setVisibility(View.GONE);
                } else {
                    holder.update_time.setVisibility(View.VISIBLE);
                    holder.update_time.setText(DateUtil.timeLogic(list.get(position).getUpdateDate()));
                    holder.huodong_show_times.setVisibility(View.GONE);
                    holder.huodong_show_times.setText(DateUtil.timeLogic(list.get(position).getUpdateDate()));
                    holder.singin_buton.setVisibility(View.VISIBLE);
                }
            }
        }
        holder.singin_buton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intentListener.onIntentClick(position);
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView hd_stick;//置顶
        TextView huodong_title;//标题
        ImageView hd_img;//图片
        TextView huodong_time;//活动时间
        TextView huodong_show_times;//隐藏签到
        TextView update_time;//更新时间
        Button singin_buton;//签到
    }

}
